package com.tjetc.service.Impl;

import com.tjetc.common.JsonResult;
import com.tjetc.dao.OrderMapper;
import com.tjetc.dao.RefundMapper;
import com.tjetc.entity.Order;
import com.tjetc.entity.Refund;
import com.tjetc.model.OrderAndRefund;
import com.tjetc.service.AlipayService;
import com.tjetc.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RefundServiceImpl implements RefundService {

    @Autowired
    private RefundMapper refundMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AlipayService alipayService;

    /***
     * 添加退款申请
     * @param refund 传入需要添加的refund对象
     * @return 返回前端处理对象
     */
    @Override
    public JsonResult add(Refund refund) {
        refundMapper.insert(refund);
        int orderId=refund.getOrderId();
        Order order = orderMapper.selectByOrderId(orderId);
        //改变订单状态
        orderMapper.changeOrderStatus(orderId,order.getStatus()+4);
        if(refund.getRefundId()!=null){
            return new JsonResult(0,"申请成功",refund);
        }else{
            return new JsonResult(1,"申请失败",refund);
        }
    }

    /***
     * 删除退款申请
     * @param id 传入需要删除的退款请求id
     * @return 返回前端显示对象
     */
    @Override
    public JsonResult delete(int id) {
        int delete = refundMapper.delete(id);
        if(delete>=0){
            return new JsonResult(0,"删除成功",delete);
        }else{
            return new JsonResult(1,"删除失败",delete);
        }
    }

    /***
     * 查询所有退款申请
     * @return 返回前端显示对象
     */
    @Override
    public JsonResult selectAll() {
        List<Refund> refunds = refundMapper.selectAll();
        if(refunds!=null){
            return new JsonResult(0,"查询成功",refunds);
        }else{
            return new JsonResult(1,"查询失败",refunds);
        }
    }

    /***
     * 根据id查询退款申请信息
     * @param id
     * @return
     */
    @Override
    public JsonResult selectById(int id) {
        Refund refund = refundMapper.selectById(id);
        if(refund!=null){
            return new JsonResult(0,"查询成功",refund);
        }else{
            return new JsonResult(1,"查询失败",refund);
        }
    }

    // 0为代付款，1为待完成，2为待送达，3为待评价，4为已完成，
    // -1为已退款，如果用户端点击退款，status应当+4，给管理端审核，如果管理员拒绝，则-4回滚，如果同意，则直接置为-1
    @Transactional
    @Override
    public void acceptRefund(Integer id) {
        Refund refund = refundMapper.selectById(id);
        if(refund == null || refund.getOrderId() == null) {
            throw new RuntimeException("查询到的退款请求异常");
        }
        Order order = orderMapper.selectByOrderId(refund.getOrderId());
        if(order.getStatus() <= 0) {
            throw new RuntimeException("退款的订单不能是未支付或者已经退款状态");
        }
        if(order.getStatus() >= 3 && order.getStatus() <= 4) {
            throw new RuntimeException("退款的订单不能是已经确认收货或者正在退款状态");
        }
        // 将状态设置为已经退款, 退款的状态只能是1,2
        // 改为直接调用alipay中的退款接口
        alipayService.refund(refund.getRefundId(), refund.getMessage());
//        int update = orderMapper.changeOrderStatus(refund.getOrderId(), -1);
//        Integer updateTime = orderMapper.chageOrderCheckoutDay(refund.getOrderId(), LocalDateTime.now());// 直接将checkoutTime设置为当前时间，代表已经对订单进行处理，处理结果为退款
//        int delete1 = refundMapper.delete(id);
//        if(delete1 <= 0 || update <= 0 || updateTime <= 0) {
//            throw new RuntimeException("删除或者更新已处理记录失败");
//        }
    }

    /**
     * 拒绝退款的时候将代付款的状态回滚-4
     * @param id 传入指定的退款申请id
     */
    @Transactional
    @Override
    public void refuseRefund(Integer id) {
        Refund refund = refundMapper.selectById(id);
        Order order = orderMapper.selectByOrderId(refund.getOrderId());
        int change = orderMapper.changeOrderStatus(order.getId(), order.getStatus() - 4);// 状态回滚
        int delete = refundMapper.delete(id);

        if(change <= 0 || delete <= 0) {
            throw new RuntimeException("删除或者更新已处理记录失败");
        }
    }

    /**
     * 列出所有需要在前端展示的退款信息，返回一个前端模型
     * @return 返回前端处理对象
     */
    @Override
    public JsonResult listRefundAndOrder() {
        List<OrderAndRefund> listRefondInfo = new ArrayList<>();
        List<Refund> refunds = refundMapper.selectAll();
        for(Refund refund : refunds) {
            OrderAndRefund orderAndRefund = new OrderAndRefund();
            orderAndRefund.setRefund(refund);
            Order order = orderMapper.selectByOrderId(refund.getOrderId());
            orderAndRefund.setOrder(order);
            listRefondInfo.add(orderAndRefund);
        }
        return new JsonResult(0, "", listRefondInfo);
    }
}
